package com.yygo.model;

import com.yygo.model.enums.*;
import org.hibernate.search.annotations.*;

import javax.persistence.*;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by tiansha on 2015/6/26.
 */
@Entity
@Table(name = "loan_person")
@Indexed
public class LoanPerson extends BaseObject {

    private static final long serialVersionUID = 7902595822519502186L;
    private Long id;
    private String name;
    private IDType idType;
    private String idNo;
    private Date dueDate;
    private String idProvince;
    private Gender gender;
    private Date birthday;
    private MaritalStatus maritalStatus;
    private Address address;
    private Address hkAddress;
    private PropertyType propertyType;
    private String workCompany;
    private Address companyAddress;
    private OccupationType occupation;
    private CompanyNature companyNature;
    private String companyType;
    private int companyYears;
    private WorkPosition workPosition;
    private float annualIncome;
    private float personIncome;
    private float coupleIncome;

    private String homeTelNo;
    private String workTelNo;
    private String mobileNo;
    private String mobileProvince;
    private String mobileCity;
    private String email;
    private MailType mailType;

    private int liveYears;
    private EducationLevel educationLevel;

    private Long reqId;
    private Date requestDate;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 50)
    @Field
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "id_type")
    public IDType getIdType() {
        return idType;
    }

    public void setIdType(IDType idType) {
        this.idType = idType;
    }

    @Column(name="id_no", length = 20)
    @Field(analyze= Analyze.NO)
    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    @Column(name="due_date")
    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    @Column(name="province_code", length = 20)
    public String getIdProvince() {
        return idProvince;
    }

    public void setIdProvince(String idProvince) {
        this.idProvince = idProvince;
    }

    @Enumerated(EnumType.STRING)
    @Column
    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    @Column
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "marital_status", length = 20)
    public MaritalStatus getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(MaritalStatus maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="address_id")
    @ContainedIn
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="hk_address_id")
    @ContainedIn
    public Address getHkAddress() {
        return hkAddress;
    }

    public void setHkAddress(Address hkAddress) {
        this.hkAddress = hkAddress;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "property_type", length = 20)
    public PropertyType getPropertyType() {
        return propertyType;
    }

    public void setPropertyType(PropertyType propertyType) {
        this.propertyType = propertyType;
    }

    @Column(name="work_company", length = 255)
    @Field
    public String getWorkCompany() {
        return workCompany;
    }

    public void setWorkCompany(String workCompany) {
        this.workCompany = workCompany;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="comp_address_id")
    @ContainedIn
    public Address getCompanyAddress() {
        return companyAddress;
    }

    public void setCompanyAddress(Address companyAddress) {
        this.companyAddress = companyAddress;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "occupation", length = 30)
    public OccupationType getOccupation() {
        return occupation;
    }

    public void setOccupation(OccupationType occupation) {
        this.occupation = occupation;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "company_nature", length = 20)
    public CompanyNature getCompanyNature() {
        return companyNature;
    }

    public void setCompanyNature(CompanyNature companyNature) {
        this.companyNature = companyNature;
    }

    @Column(name="company_type", length = 50)
    public String getCompanyType() {
        return companyType;
    }

    public void setCompanyType(String companyType) {
        this.companyType = companyType;
    }

    @Column(name="company_years")
    public int getCompanyYears() {
        return companyYears;
    }

    public void setCompanyYears(int companyYears) {
        this.companyYears = companyYears;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "work_position", length = 20)
    public WorkPosition getWorkPosition() {
        return workPosition;
    }

    public void setWorkPosition(WorkPosition workPosition) {
        this.workPosition = workPosition;
    }

    @Column(name="annual_income")
    public float getAnnualIncome() {
        return annualIncome;
    }

    public void setAnnualIncome(float annualIncome) {
        this.annualIncome = annualIncome;
    }

    @Column(name="person_income")
    public float getPersonIncome() {
        return personIncome;
    }

    public void setPersonIncome(float personIncome) {
        this.personIncome = personIncome;
    }
    @Column(name="couple_income")
    public float getCoupleIncome() {
        return coupleIncome;
    }

    public void setCoupleIncome(float coupleIncome) {
        this.coupleIncome = coupleIncome;
    }

    @Column(name="home_tel_no", length = 20)
    @Field(analyze= Analyze.NO)
    public String getHomeTelNo() {
        return homeTelNo;
    }

    public void setHomeTelNo(String homeTelNo) {
        this.homeTelNo = homeTelNo;
    }

    @Column(name="work_tel_no", length = 20)
    @Field(analyze= Analyze.NO)
    public String getWorkTelNo() {
        return workTelNo;
    }

    public void setWorkTelNo(String workTelNo) {
        this.workTelNo = workTelNo;
    }

    @Column(name="mobile_no", length = 20)
    @Field(analyze= Analyze.NO)
    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

    @Column(length = 50)
    @Field
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "mail_type", length = 20)
    public MailType getMailType() {
        return mailType;
    }

    public void setMailType(MailType mailType) {
        this.mailType = mailType;
    }

    @Column(name = "mobile_province", length = 20)
    public String getMobileProvince() {
        return mobileProvince;
    }

    public void setMobileProvince(String mobileProvince) {
        this.mobileProvince = mobileProvince;
    }

    @Column(name = "mobile_city", length = 20)
    public String getMobileCity() {
        return mobileCity;
    }

    public void setMobileCity(String mobileCity) {
        this.mobileCity = mobileCity;
    }

    @Column(name="req_id")
    public Long getReqId() {
        return reqId;
    }

    public void setReqId(Long reqId) {
        this.reqId = reqId;
    }

    @Column(name="request_date")
    public Date getRequestDate() {
        return requestDate;
    }

    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    @Transient
    public int getAge() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(birthday);
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR) - calendar.get(Calendar.YEAR) + 1;
    }

    @Column(name="live_years")
    public int getLiveYears() {
        return liveYears;
    }

    public void setLiveYears(int liveYears) {
        this.liveYears = liveYears;
    }

    @Enumerated(EnumType.STRING)
    @Column(name="education_level")
    public EducationLevel getEducationLevel() {
        return educationLevel;
    }

    public void setEducationLevel(EducationLevel educationLevel) {
        this.educationLevel = educationLevel;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LoanPerson loanPerson = (LoanPerson) o;

        if (getId().equals(loanPerson.getId())) {
            return true;
        }

        return false;
    }

    public int hashCode() {
        int result;
        result = (getName() != null ? getName().hashCode() : 0);
        result = 31 * result + (getIdType() != null ? getIdType().hashCode() : 0);
        result = 57 * result + (getIdNo() != null ? getIdNo().hashCode() : 0);
        return result;
    }

    public String toString() {
        return "LoanPerson{" +
                ", name='" + getName() + '\'' +
                ", idType='" + getIdType() + '\'' +
                ", idNo='" + getIdNo() + '\'' +
                '}';
    }
}
